<template>
  <div class="container">
    <header>
      <h1>博客列表</h1>
      <p class="muted">共 {{ metas.length }} 篇文章</p>
    </header>

    <el-card class="toolbar" shadow="never">
      <div class="filters">
        <el-select v-model="selectedTags" multiple collapse-tags placeholder="选择标签" style="min-width: 260px;">
          <el-option v-for="t in allTags" :key="t" :label="t" :value="t" />
        </el-select>
        <el-button @click="clearTags" plain type="primary">清空</el-button>
      </div>
    </el-card>

    <div class="posts-grid">
      <div v-for="post in filtered" :key="post.slug" class="post-card">
        <RouterLink :to="{ name: 'post', params: { slug: post.slug } }">
          <div class="card-inner">
            <div v-if="post.cover" class="post-img" :style="{ backgroundImage: `url(${post.cover})` }">
              <div class="img-overlay">
                <i class="fas fa-book-open"></i>
              </div>
            </div>
            <div v-else class="post-img post-img-default">
              <i class="fas fa-code"></i>
            </div>
            <div class="post-content">
              <span v-if="post.date" class="post-date">
                <i class="far fa-calendar-alt"></i>
                {{ post.date }}
              </span>
              <h3 class="post-title">{{ post.title }}</h3>
              <p class="post-excerpt" v-if="post.excerpt">
                {{ post.excerpt }}{{ post.excerpt.length >= 120 ? '...' : '' }}
              </p>
              <div class="post-meta">
                <span v-if="post.readingMinutes" class="read-time">
                  <i class="far fa-clock"></i>
                  {{ post.readingMinutes }} 分钟
                </span>
                <div v-if="post.tags?.length" class="post-tags">
                  <span v-for="t in post.tags" :key="t" class="tag-item"># {{ t }}</span>
                </div>
              </div>
              <div class="read-more">
                阅读更多 <i class="fas fa-arrow-right"></i>
              </div>
            </div>
          </div>
        </RouterLink>
      </div>
    </div>
  </div>
  
</template>

<script setup lang="ts">
import { onMounted, computed, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { usePostsStore } from '@/stores/posts';

const store = usePostsStore();
const route = useRoute();
const router = useRouter();
const metas = computed(() => store.metas);
const allTags = computed(() => store.allTags);
const selectedTags = ref<string[]>([]);

const filtered = computed(() => {
  if (!selectedTags.value.length) return metas.value;
  return metas.value.filter(m => m.tags && selectedTags.value.every(t => m.tags!.includes(t)));
});

function clearTags() { selectedTags.value = []; }

onMounted(() => {
  store.loadMetas();
  const q = route.query.tags;
  if (typeof q === 'string' && q.trim()) {
    selectedTags.value = q.split(',').map(s => s.trim()).filter(Boolean);
  }
});

watch(selectedTags, (val) => {
  const tags = val.length ? val.join(',') : undefined;
  router.replace({ query: { ...route.query, tags } });
});
</script>

<style scoped>
.container { 
  max-width: 1200px; 
  margin: 0 auto; 
  padding: 2rem 1rem; 
}

header { 
  margin-bottom: 2rem; 
  text-align: center;
}

header h1 {
  font-family: 'Orbitron', monospace;
  font-size: 2.5rem;
  background: linear-gradient(135deg, var(--primary), var(--secondary));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  margin-bottom: 10px;
}

.muted { 
  color: var(--muted); 
  font-size: 1.1rem;
}

.toolbar { 
  background: var(--glass); 
  border: 1px solid var(--glass-border); 
  backdrop-filter: blur(15px);
  margin-bottom: 2rem; 
  transition: all 0.3s ease;
}

.toolbar:hover {
  background: rgba(37, 99, 235, 0.05);
}

.filters { 
  display: flex; 
  gap: 1rem; 
  align-items: center; 
}

.posts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(380px, 1fr));
  gap: 30px;
}

.post-card {
  transition: all 0.4s ease;
}

.post-card:hover {
  transform: translateY(-10px);
}

.card-inner {
  background: var(--glass);
  border: 1px solid var(--glass-border);
  border-radius: 20px;
  overflow: hidden;
  transition: all 0.4s ease;
  backdrop-filter: blur(15px);
  height: 100%;
  display: flex;
  flex-direction: column;
}

.card-inner:hover {
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
  border-color: rgba(37, 99, 235, 0.3);
}

.post-img {
  height: 220px;
  background-size: cover;
  background-position: center;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.post-img-default {
  background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
  font-size: 3rem;
  color: white;
}

.img-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: all 0.3s ease;
  font-size: 2rem;
  color: white;
}

.post-card:hover .img-overlay {
  opacity: 1;
}

.post-content {
  padding: 25px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.post-date {
  color: var(--primary);
  font-size: 0.9rem;
  margin-bottom: 15px;
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 500;
}

.post-title {
  font-family: 'Orbitron', monospace;
  font-size: 1.3rem;
  font-weight: 600;
  margin-bottom: 15px;
  color: var(--light);
  line-height: 1.4;
}

.post-excerpt {
  color: var(--gray);
  font-size: 0.95rem;
  line-height: 1.6;
  margin-bottom: 20px;
  flex: 1;
}

.post-meta {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 10px;
}

.read-time {
  color: var(--gray);
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 4px;
}

.post-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag-item {
  color: var(--primary);
  font-size: 0.8rem;
  background: rgba(37, 99, 235, 0.1);
  padding: 4px 8px;
  border-radius: 12px;
  border: 1px solid rgba(37, 99, 235, 0.2);
  font-weight: 500;
}

.read-more {
  color: var(--primary);
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.95rem;
  margin-top: auto;
  transition: all 0.3s ease;
}

.read-more i {
  transition: transform 0.3s ease;
}

.post-card:hover .read-more {
  color: var(--secondary);
}

.post-card:hover .read-more i {
  transform: translateX(5px);
}

@media (max-width: 768px) {
  .posts-grid {
    grid-template-columns: 1fr;
    gap: 20px;
  }
  
  header h1 {
    font-size: 2rem;
  }
  
  .filters {
    flex-direction: column;
    align-items: stretch;
  }
  
  .post-img {
    height: 180px;
  }
  
  .post-content {
    padding: 20px;
  }
}

@media (max-width: 576px) {
  .container {
    padding: 1rem 0.5rem;
  }
  
  header h1 {
    font-size: 1.8rem;
  }
  
  .post-content {
    padding: 15px;
  }
}
</style>

